package com.fisherman.condition;

import com.fisherman.bean.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @User: fisherman
 * @Date: 2020/5/20 9:33
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * @param importingClassMetadata 当前类的注解信息
     * @param registry               Bean的注册类
     *                               把所有需要添加到容器的bean，调用
     *                               BeanDefinitionRegistry.registerBeanDefinition方法手动注册
     */
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //判断下容器中是否有 red
        boolean definition = registry.containsBeanDefinition("com.fisherman.bean.Red");
        //判断下容器中是否有 red
        boolean definition1 = registry.containsBeanDefinition("com.fisherman.bean.Blue");

        if (definition && definition1){
            //指定bean定义信息（Bean的类型，bean的作用域。。）
            RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
            //注册一个Bean，指定bean名
            registry.registerBeanDefinition("rainBow",beanDefinition);
        }
    }
}
